package com.loveyukari.controller;


import com.loveyukari.common.exception.InternalException;
import com.loveyukari.model.Auth;
import com.loveyukari.service.ManagerService;
import com.loveyukari.service.TourService;
import com.loveyukari.support.JsonResult;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import sun.rmi.runtime.Log;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class TourController {

    private Logger logger  = LoggerFactory.getLogger(TourController.class);

    @Autowired
    private TourService tourService;
    @Autowired
    private ManagerService managerService;














    @RequestMapping("/admin/tour/ticketCode")
    public String ticketCode(HttpServletRequest request){
        String sign = (String) request.getAttribute("sign");
        int authority = managerService.getManageAuthority(sign);
        if (authority > 5){
            return "redirect:/admin/index";
        }
        return "tour/ticket_code";
    }

    @RequestMapping("/admin/tour/checkTicketCode")
    @ResponseBody
    public JsonResult checkTicketCode(String ticketCode,String city,HttpServletRequest request){
        String sign = (String) request.getAttribute("sign");
        int authority = managerService.getManageAuthority(sign);
        if (authority > 5){
            return new JsonResult(1,"您的权限不足");
        }
        try {
            JSONObject json = tourService.checkTicketCode(ticketCode, city);
            return new JsonResult(0,"",json);
        } catch (InternalException e) {
            return new JsonResult(1,e.getMessage());
        } catch (Exception e){
            logger.error(e.getMessage(),e);
            return new JsonResult(-1,"服务器异常");
        }
    }




    @RequestMapping("/admin/tour/ticketCode/index")
    public String ticketCodeIndex(HttpServletRequest request){
        String sign = (String) request.getAttribute("sign");
        int authority = managerService.getManageAuthority(sign);
        if (authority > 5){
            return "redirect:/admin/index";
        }
        return "tour/create_ticket_code";
    }

    @RequestMapping("/admin/tour/createTicketCode")
    @ResponseBody
    public JsonResult ticketCode(int size,String city,HttpServletRequest request){
        String sign = (String) request.getAttribute("sign");
        int authority = managerService.getManageAuthority(sign);
        if (authority > 5){
            return new JsonResult(1,"您的权限不足");
        }
        List<String> list = tourService.batchCreateTicketCode(size, city);
        return new JsonResult(0,"",list);
    }



    @RequestMapping("/admin/tour/qr")
    public String qr(HttpServletRequest request){
        String sign = (String) request.getAttribute("sign");
        int authority = managerService.getManageAuthority(sign);
        if (authority > 5){
            return "redirect:/admin/index";
        }
        return "tour/qr";
    }

    @RequestMapping("/admin/tour/checkQr")
    @ResponseBody
    public JsonResult checkQr(String ticket,String city,HttpServletRequest request){
        String sign = (String) request.getAttribute("sign");
        int authority = managerService.getManageAuthority(sign);
        if (authority > 5){
            return new JsonResult(1,"您的权限不足");
        }
        try {
            JSONObject json = tourService.checkQr(ticket, city);
            return new JsonResult(0,"",json);
        } catch (InternalException e) {
            return new JsonResult(1,e.getMessage());
        } catch (Exception e){
            logger.error(e.getMessage(),e);
            return new JsonResult(-1,"服务器异常");
        }
    }

    @RequestMapping("/admin/tour/checkCode")
    @ResponseBody
    public JsonResult checkCode(String code,String city,HttpServletRequest request){
        String sign = (String) request.getAttribute("sign");
        int authority = managerService.getManageAuthority(sign);
        if (authority > 5){
            return new JsonResult(1,"您的权限不足");
        }
        try {
            JSONObject json = tourService.checkCode(code, city);
            return new JsonResult(0,"",json);
        } catch (InternalException e) {
            return new JsonResult(1,e.getMessage());
        } catch (Exception e){
            logger.error(e.getMessage(),e);
            return new JsonResult(-1,"服务器异常");
        }
    }

    @RequestMapping("/admin/tour/code")
    public String code(HttpServletRequest request){
        String sign = (String) request.getAttribute("sign");
        int authority = managerService.getManageAuthority(sign);
        if (authority > 5){
            return "redirect:/admin/index";
        }
        return "tour/code";
    }










    @RequestMapping("/tour/add")
    @ResponseBody
    public JsonResult add(String douyuName,String qq,String phone,String city,String ip){
        try {
            JSONObject json = tourService.addTourUser(douyuName, qq, phone, city,ip);
            return new JsonResult(0,"",json);
        } catch (InternalException e) {
            return new JsonResult(1,e.getMessage());
        } catch (Exception e){
            logger.error(e.getMessage(),e);
            return new JsonResult(-1,"服务器异常，请稍后再试");
        }
    }

    @RequestMapping("/tour/cancel")
    @ResponseBody
    public JsonResult add(String token,String city){
        try {
            JSONObject json = tourService.cancel(token, city);
            return new JsonResult(0,"",json);
        } catch (InternalException e) {
            return new JsonResult(1,e.getMessage());
        } catch (Exception e){
            logger.error(e.getMessage(),e);
            return new JsonResult(-1,"服务器异常，请稍后再试");
        }
    }



    @RequestMapping("/tour/getUserInfo")
    @ResponseBody
    public JsonResult getUserInfo(String token,String phone, String city){
        try {
            if (StringUtils.isNotBlank(token)) {
                JSONObject json = tourService.getTourUserByToken(token, city);
                return new JsonResult(0, "", json);
            } else if (StringUtils.isNotBlank(phone)) {
                JSONObject json = tourService.getTourUserByPhone(phone, city);
                return new JsonResult(0, "", json);
            } else {
                return new JsonResult(0, "", new JSONObject());
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return new JsonResult(-1,"服务器异常");
        }
    }


    @RequestMapping("/admin/tour/list")
    public String list(Model model, String city, @RequestParam(defaultValue = "-10") int status, String name, HttpServletRequest request){
        String sign = (String) request.getAttribute("sign");
        int authority = managerService.getManageAuthority(sign);
        if (authority > 5){
            return "redirect:/admin/index";
        }
        if (StringUtils.isBlank(city)){
            city = "chengdu";
        }
        List<JSONObject> list = tourService.list(city);
        model.addAttribute("list",list);
        model.addAttribute("city",city);
        model.addAttribute("size",list.size());
        model.addAttribute("status",status);
        model.addAttribute("name",name);
        return "tour/list";
    }

    @RequestMapping("/admin/tour/ticket/update")
    @ResponseBody
    public JsonResult ticketUpdate(String city,int tourId,int status,HttpServletRequest request){
        String sign = (String) request.getAttribute("sign");
        int authority = managerService.getManageAuthority(sign);
        if (authority > 5){
            return new JsonResult(1,"您的权限不足");
        }
        tourService.updateTicket(tourId,city,status);
        return new JsonResult();
    }


    @RequestMapping("/admin/tour/exclude/update")
    @ResponseBody
    public JsonResult excludeUpdate(int exclude,int id,HttpServletRequest request){
        String sign = (String) request.getAttribute("sign");
        int authority = managerService.getManageAuthority(sign);
        if (authority > 5){
            return new JsonResult(1,"您的权限不足");
        }
        tourService.updateExclude(id,exclude);
        return new JsonResult();
    }



    @RequestMapping(value="/admin/tour/draw/param")
    public String draw(){
        return "tour/tour_create_url";
    }


    @RequestMapping(value="/admin/tour/draw/index")
    public String draw(Model model,int size, String city, HttpServletRequest request){
        model.addAttribute("size",size);
        List<Auth> list = new ArrayList<>();
        if ("jijiu".equals(city)){
            model.addAttribute("status",1);
            model.addAttribute("city","shanghai");
            list = tourService.getCityAuth("shanghai",1);
        } else {
            model.addAttribute("status",0);
            model.addAttribute("city",city);
            list = tourService.getCityAuth(city,0);
        }
        model.addAttribute("list",list);
        return "tour/tour_index";
    }

    @RequestMapping("/admin/tour/draw")
    @ResponseBody
    public JsonResult draw(String city,Integer size,Integer status, HttpServletRequest request){
        if (StringUtils.isBlank(city)){
            return new JsonResult(1,"请选择抽奖场次");
        }
        if (size == null){
            return new JsonResult(1,"请输入抽奖人数");
        }
        String sign = (String) request.getAttribute("sign");
        if (StringUtils.isBlank(sign)){
            return new JsonResult(1,"请先登录");
        }
        int authority = managerService.getManageAuthority(sign);
        if (authority > 5){
            return new JsonResult(1,"您的权限不足");
        }
        try {
            List<Auth> list = tourService.draw(size, city,status);
            return new JsonResult(0,"",list);
        } catch (InternalException e) {
            return new JsonResult(1,e.getMessage());
        } catch (Exception e){
            logger.error(e.getMessage(),e);
            return new JsonResult(-1,"出错啦~快找小马");
        }
    }






}
